//
//  RecommendView.m
//  phmall
//
//  Created by admin on 2017/10/18.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "RecommendView.h"
#import "SeckillCell.h"
#import "GoodsListCell.h"
#import "CategoryModel.h"
#import "GoodsModel.h"
@interface RecommendView ()<UICollectionViewDataSource,UICollectionViewDelegate,GoodsListCellDelegate>
@property(nonatomic,strong)UILabel *title;
@property(nonatomic,strong)UIView *Lline;
@property(nonatomic,strong)UIView *Rline;
@end

@implementation RecommendView

-(instancetype)init{
    if (self=[super init]) {
        self.title=[[UILabel alloc] init];
        self.Lline=[[UIView alloc]init];
        self.Rline=[[UIView alloc]init];
    
        [self addSubview:self.title];
        self.title.text=@"猜你喜欢";
        self.title.font=[UIFont systemFontOfSize:13];
        self.title.textColor=[UIColor colorWithHexString:@"#adadad"];
      
        
        [self addSubview:self.Lline];
        self.Lline.backgroundColor=[UIColor colorWithHexString:@"#adadad"];
        [self addSubview:self.Rline];
        self.Rline.backgroundColor=[UIColor colorWithHexString:@"#adadad"];
        
        [self addSubview:self.collectionView];
        self.collectionView.scrollEnabled = NO;
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    CGSize superSize = self.frame.size;
    
    if (CGSizeEqualToSize(superSize, CGSizeZero)) {
        return;
    }

    [self.title mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(0);
        make.centerX.equalTo(self);
        make.height.offset(40);
    }];
    [self.Lline mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.title);
        make.width.offset(40);
        make.height.offset(1);
        make.right.equalTo(self.title.mas_left).offset(-10);
    }];
    [self.Rline mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.title);
        make.width.offset(40);
        make.height.offset(1);
        make.left.equalTo(self.title.mas_right).offset(10);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.title.mas_bottom).offset(0);
        make.bottom.equalTo(self).offset(0);
    }];
}

#pragma mark collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.listArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId=@"cell";
    GoodsListCell *cell = (GoodsListCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    cell.model=self.listArr[indexPath.row];
    cell.layer.cornerRadius = 10;
    cell.delegate=self;
    cell.contentView.layer.cornerRadius = 10.0f;
    cell.contentView.layer.masksToBounds = YES;
    return cell;
}

//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (iPhone5SE) {
        return CGSizeMake(SCREEN_WIDTH/2-15, 230);
    }else  if (iPhone6_6s) {
        return CGSizeMake(SCREEN_WIDTH/2-15, 252);
    }else  if (iPhone6Plus_6sPlus) {
        return CGSizeMake(SCREEN_WIDTH/2-15, 272);
    }else{
        return CGSizeMake(SCREEN_WIDTH/2-15, 252);
    }
}
//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}
//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 10, 10, 10);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    for (int i=0; i<self.listArr.count; i++) {
        NSIndexPath *index1 =  [NSIndexPath indexPathForItem:i inSection:0];
        GoodsListCell *cell =  (GoodsListCell *)[_collectionView cellForItemAtIndexPath:index1];
        [cell moreViewTapAction];
    }
    
    GoodsModel *model=self.listArr[indexPath.row];
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectItemAtIndexPath:)]) {
        [self.delegate didSelectItemAtIndexPath:model.uid];
    }
}
-(void)moreAction:(UICollectionViewCell *)cell{
    for (int i=0; i<self.listArr.count; i++) {
        NSIndexPath *index1 =  [NSIndexPath indexPathForItem:i inSection:0];
        GoodsListCell *goodsCell=(GoodsListCell *)[self.collectionView cellForItemAtIndexPath:index1];
        if (cell!=goodsCell) {
            [goodsCell moreViewTapAction];
        }
    }
}
-(UICollectionView *)collectionView{
    if (!_collectionView) {
        //1.初始化layout
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        //设置collectionView滚动方向
        //    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        //设置headerView的尺寸大小
        //        layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 40);
  
        //2.初始化collectionView
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        
        //3.注册collectionViewCell
        //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
        [_collectionView registerClass:[GoodsListCell class] forCellWithReuseIdentifier:@"cell"];
        
        _collectionView.showsVerticalScrollIndicator=NO;
        
        //4.设置代理
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

-(void)setListArr:(NSMutableArray *)listArr{
    _listArr=listArr;
    [self.collectionView reloadData];
}

@end

